import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/'

import CustomerEdit from '@/components/CustomerEdit'
import Customer from '@/components/Customer'
import Repair from '@/components/Repair'
import RepairEdit from '@/components/RepairEdit'
import RepairInfo from '@/components/RepairInfo'
import RepairSend from '@/components/RepairSend'
import RepairDo from '@/components/RepairDo'
import RepairSign from '@/components/RepairSign'
import RepairEval from '@/components/RepairEval'
import Me from '@/components/Me'
import Test from '@/components/Test'
import Login from '@/components/Login'
import Contact from '@/components/Contact'
import ContactEdit from '@/components/ContactEdit'
import Batch from '@/components/Batch'
import BatchEdit from '@/components/BatchEdit'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'Customer',
      component: Customer
    },
    {
      path: '/customer-edit',
      name: 'Custom-edit',
      component: CustomerEdit
    },
    {
      path: '/customer',
      name: 'customer',
      component: Customer
    },
    {
      path: '/repair',
      name: 'repair',
      component: Repair
    },
    {
      path: '/repair-edit',
      name: 'repair-edit',
      component: RepairEdit
    },
    {
      path: '/repair-info',
      name: 'repair-info',
      component: RepairInfo
    },
    {
      path: '/repair-send',
      name: 'repair-send',
      component: RepairSend
    },
    {
      path: '/repair-do',
      name: 'repair-do',
      component: RepairDo
    },
    {
      path: '/repair-sign',
      name: 'repair-sign',
      component: RepairSign,
    },
    {
      path: '/repair-eval',
      name: 'repair-eval',
      component: RepairEval,
    },
    {
      path: '/me',
      name: 'me',
      component: Me
    },
    {
      path: '/test',
      name: 'test',
      component: Test
    },
    {
      path: '/contact',
      name: 'contact',
      component: Contact
    },
    {
      path: '/contact-edit',
      name: 'contact-edit',
      component: ContactEdit
    },
    {
      path: '/batch',
      name: 'batch',
      component: Batch
    },
    {
      path: '/batch-edit',
      name: 'batch-edit',
      component: BatchEdit
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { noCheckToken: true },
    }
  ]
})

// simple history management
const history = window.sessionStorage
history.clear()
let historyCount = history.getItem('count') * 1 || 0
history.setItem('/', 0)

router.beforeEach(function (to, from, next) {
  store.commit('updateLoadingStatus', { isLoading: true })

  //登陆校验
  let token = sessionStorage.isLogin || ''
  let noCheckToken = to.matched.some(record => record.meta.noCheckToken)
  if (!noCheckToken && !token) {
    next('/login')
    return
  }

  const toIndex = history.getItem(to.path)
  const fromIndex = history.getItem(from.path)

  if (toIndex) {
    if (toIndex > fromIndex || !fromIndex || (toIndex === '0' && fromIndex === '0')) {
      store.commit('updateDirection', { direction: 'forward' })
    } else {
      store.commit('updateDirection', { direction: 'reverse' })
    }
  } else {
    ++historyCount
    history.setItem('count', historyCount)
    to.path !== '/' && history.setItem(to.path, historyCount)
    store.commit('updateDirection', { direction: 'forward' })
  }
  next()
})

router.afterEach(function (to) {
  store.commit('updateLoadingStatus', { isLoading: false })
})

export default router